package chat.ourGamePackage3;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;


public class Entity
{
	private Sprite sprite;
	private int x;
	private int y;
	private BufferedImage sourceImage = null;
	
	public Entity(String ref, int x, int y)
	{
		// Gets the sprite from SpriteStore class
		// TODO Fix the spritestore to cache loaded images?
		//this.sprite = SpriteStore.get().getSprite(ref);
		
		this.x = x;
		this.y = y;
		
		// Test drawing a sprite loaded from disk
		setImage(ref);
	}
	
	public void setImage(String resource)
	{
		// Test drawing a sprite loaded from disk
		URL url = Game.class.getResource(resource);
		try
		{
			sourceImage = ImageIO.read(url);
		} 
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// create an accelerated image of the right size to store our sprite in
		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		Image image = gc.createCompatibleImage(sourceImage.getWidth(), sourceImage.getHeight(), Transparency.BITMASK);
		
		// draw our source image into the accelerated image
		image.getGraphics().drawImage(sourceImage, 0,0,null);
		
		// create a sprite, add it the cache then return it
		this.sprite = new Sprite(image);
	}
	
	public void moveLeft(int x)
	{		
		setImage("sprites/marioLeft.png");
		this.x -= x;
	}
	
	public void moveRight(int y)
	{
		setImage("sprites/mario.png");
		this.x += y;
	}
	
	public void Draw(Graphics g)
	{
		sprite.draw(g, this.x, this.y);
	}
}
